package com.ems.sys.controller;

import com.ems.entity.Result;
import com.ems.entity.StatusCode;
import com.ems.sys.pojo.Employee;
import com.ems.sys.service.EmployeeService;
import com.github.pagehelper.PageInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:传智播客
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/sys/employee")
//@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService emlpoyeeService;

    /***
     * Emlpoyee分页条件搜索实现
     * @param emlpoyee
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) Employee emlpoyee, @PathVariable int page, @PathVariable int size) {

        //调用EmlpoyeeService实现分页条件查询Emlpoyee
        PageInfo<Employee> pageInfo = emlpoyeeService.findPage(emlpoyee, page, size);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     /***
     * Emlpoyee分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        //调用EmlpoyeeService实现分页查询Emlpoyee
        PageInfo<Employee> pageInfo = emlpoyeeService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param emlpoyee
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Employee>> findList(@RequestBody(required = false) Employee emlpoyee) {
        //调用EmlpoyeeService实现条件查询Emlpoyee
        List<Employee> list = emlpoyeeService.findList(emlpoyee);
        return new Result<List<Employee>>(true, StatusCode.OK, "查询成功", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        //调用EmlpoyeeService实现根据主键删除
        emlpoyeeService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 修改Emlpoyee数据
     * @param emlpoyee
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Employee emlpoyee, @PathVariable String id) {
        //设置主键值
        emlpoyee.seteId(id);
        //调用EmlpoyeeService实现修改Emlpoyee
        emlpoyeeService.update(emlpoyee);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 新增Emlpoyee数据
     * @param emlpoyee
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Employee emlpoyee) {
        //调用EmlpoyeeService实现添加Emlpoyee
        emlpoyeeService.add(emlpoyee);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询Emlpoyee数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> findById(@PathVariable String id) {
        //调用EmlpoyeeService实现根据主键查询Emlpoyee
        Employee employee = emlpoyeeService.findById(id);
        return new Result<Employee>(true, StatusCode.OK, "查询成功", employee);
    }

    /***
     * 查询Emlpoyee全部数据
     * @return
     */
    @GetMapping
    public Result<List<Employee>> findAll() {
        //调用EmlpoyeeService实现查询所有Emlpoyee
        List<Employee> list = emlpoyeeService.findByEId();
        return new Result<List<Employee>>(true, StatusCode.OK, "查询成功", list);
    }
}
